package org.zk.dubbo_consumer.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.zk.entity.Danmup;
import org.zk.entity.Lession;
import org.zk.service.DanmuService;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/danmu")
public class DanmuController {

    @Resource
    protected DanmuService danmuService;


    @RequestMapping(value = "/sendDanmup")
    @ResponseBody
    public void  sendDanmup(HttpServletRequest request) throws IOException {
        //获取弹幕内容，包括颜色，大小，内容，位置，时间
        String danmup  =  request.getParameter("danmu");
        System.out.println(danmup);
        //获取视频id
        int id = Integer.parseInt(request.getParameter("id"));
        String[] danmups = danmup.split(",");
        String text = danmups[0].split(":")[1].substring(1,danmups[0].split(":")[1].length()-1);
        String color = danmups[1].split(":")[1].substring(1,danmups[1].split(":")[1].length()-1);
        String size = danmups[2].split(":")[1].substring(1,danmups[2].split(":")[1].length()-1);
        String position = danmups[3].split(":")[1].substring(1,danmups[3].split(":")[1].length()-1);
        String time  = danmups[4].split(":")[1].substring(0,danmups[4].split(":")[1].length()-1);
        int size1=Integer.parseInt(size);
        int position1=Integer.parseInt(position);
        Lession lession=new Lession ();
        lession.setLessionId ( id );
        Danmup danmup1 = new Danmup();
        danmup1.setColor(color);
        danmup1.setText(text);
        danmup1.setSize(size1);
        danmup1.setPosition(position1);
        danmup1.setTime(time);
        danmup1.setLession (lession);


        String time1 = formatSecondDateTime(Integer.parseInt(time));
        Danmup danmup2 = new Danmup();

        danmup2.setColor(color);
        danmup2.setText(text);
        danmup2.setSize(size1);
        danmup2.setPosition(position1);
        danmup2.setTime(time1);
        danmup2.setLession (lession);
        //存数据库
        boolean flag = danmuService.add(danmup1);
        if(flag){
            System.out.println("添加成功");
        }else{
            System.out.println("添加失败");
        }
    }

    /**
     * 接收弹幕
     *
     * @return
     */
    @RequestMapping("/receiveDanmup")
    public String findDanmups(HttpServletRequest request) {
        int id = Integer.parseInt(request.getParameter("id"));
        System.out.println(id);
        List<Danmup> danmupList = danmuService.queryLesson(id);
        System.out.println(danmupList.size());
        String jsonString = "[";
        if (danmupList.size() != 0 && danmupList != null) {
            if (danmupList.size() == 1) {
                jsonString = jsonString + "\'{\"text\":\""
                        + danmupList.get(0).getText() + "\",\"color\":\""
                        + danmupList.get(0).getColor() + "\",\"size\":\""
                        + danmupList.get(0).getSize() + "\",\"position\":\""
                        + danmupList.get(0).getPosition() + "\",\"time\":"
                        + danmupList.get(0).getTime() + "}\']";
            } else {
                for (int i = 0, len = danmupList.size(); i < len; i++) {
                    if (i == len - 1) {
                        jsonString = jsonString + "\'{\"text\":\""
                                + danmupList.get(i).getText() + "\",\"color\":\""
                                + danmupList.get(i).getColor() + "\",\"size\":\""
                                + danmupList.get(i).getSize() + "\",\"position\":\""
                                + danmupList.get(i).getPosition() + "\",\"time\":"
                                + danmupList.get(i).getTime() + "}\']";
                    } else {
                        jsonString = jsonString + "\'{\"text\":\""
                                + danmupList.get(i).getText() + "\",\"color\":\""
                                + danmupList.get(i).getColor() + "\",\"size\":\""
                                + danmupList.get(i).getSize() + "\",\"position\":\""
                                + danmupList.get(i).getPosition() + "\",\"time\":"
                                + danmupList.get(i).getTime() + "}\',";
                    }
                }
            }
        }
        System.out.println(jsonString);
        return jsonString;
    }

    @RequestMapping(value = "/queryDanmup")
    public List<Danmup> queryDanmup(HttpServletRequest request){
        int id = Integer.parseInt(request.getParameter("id"));
        List<Danmup> danmupList = danmuService.queryLesson(id);
        List<Danmup> danmups = new ArrayList<> ();
        for (Danmup d:danmupList) {
            Danmup danmup = new Danmup();
            String time= formatSecondDateTime(Integer.parseInt(d.getTime()));
            danmup.setCode(d.getCode());
            danmup.setText(d.getText());
            danmup.setSize(d.getSize());
            danmup.setColor(d.getColor());
            danmup.setPosition(d.getPosition());
            danmup.setLession (d.getLession ());
            danmup.setTime(time);
            danmups.add(danmup);
        }
        return danmups;
    }
    public static String formatSecondDateTime(int date) {
        if (date <= 0) {
            return "";
        }
        int scound = date/10;
        int h = scound / 3600;
        int m = scound % 3600 / 60;
        int s = scound % 60; //不足60的就是秒，够60就是分
        return h + ":" + m + ":" + s;
    }
}
